# HarmonyOS 开发环境检查脚本

Write-Host "================================" -ForegroundColor Cyan
Write-Host "  HarmonyOS 开发环境检查工具" -ForegroundColor Cyan
Write-Host "================================" -ForegroundColor Cyan
Write-Host ""

# 检查 DevEco Studio 常见安装路径
$commonPaths = @(
    "$env:LOCALAPPDATA\Huawei\DevEco Studio",
    "C:\Program Files\Huawei\DevEco Studio",
    "$env:ProgramFiles\Huawei\DevEco Studio",
    "${env:ProgramFiles(x86)}\Huawei\DevEco Studio"
)

Write-Host "1. 检查 DevEco Studio 安装..." -ForegroundColor Yellow
$devEcoPath = $null
foreach ($path in $commonPaths) {
    if (Test-Path $path) {
        Write-Host "  ✅ 找到 DevEco Studio: $path" -ForegroundColor Green
        $devEcoPath = $path
        break
    }
}

if ($null -eq $devEcoPath) {
    Write-Host "  ❌ 未找到 DevEco Studio 安装目录" -ForegroundColor Red
    Write-Host "     请手动查找 DevEco Studio 的安装路径" -ForegroundColor Yellow
}

Write-Host ""
Write-Host "2. 检查命令行工具..." -ForegroundColor Yellow

# 检查 hdc
$hdcPath = Get-Command hdc -ErrorAction SilentlyContinue
if ($hdcPath) {
    Write-Host "  ✅ hdc: $($hdcPath.Source)" -ForegroundColor Green
} else {
    Write-Host "  ❌ hdc 未找到" -ForegroundColor Red
    
    # 尝试查找 hdc
    $hdcLocations = @(
        "$env:LOCALAPPDATA\Huawei\Sdk\openharmony\*\toolchains\hdc.exe",
        "C:\Users\*\Huawei\Sdk\openharmony\*\toolchains\hdc.exe"
    )
    
    foreach ($pattern in $hdcLocations) {
        $found = Get-ChildItem $pattern -ErrorAction SilentlyContinue | Select-Object -First 1
        if ($found) {
            Write-Host "     💡 找到 hdc: $($found.FullName)" -ForegroundColor Yellow
            Write-Host "     需要将此目录添加到 PATH: $($found.Directory.FullName)" -ForegroundColor Yellow
            break
        }
    }
}

# 检查 ohpm
$ohpmPath = Get-Command ohpm -ErrorAction SilentlyContinue
if ($ohpmPath) {
    Write-Host "  ✅ ohpm: $($ohpmPath.Source)" -ForegroundColor Green
} else {
    Write-Host "  ❌ ohpm 未找到" -ForegroundColor Red
    
    # 尝试查找 ohpm
    $ohpmLocations = @(
        "$env:LOCALAPPDATA\Huawei\Sdk\openharmony\*\toolchains\ohpm.exe",
        "C:\Users\*\Huawei\Sdk\openharmony\*\toolchains\ohpm.exe",
        "$env:APPDATA\npm\ohpm.cmd"
    )
    
    foreach ($pattern in $ohpmLocations) {
        $found = Get-ChildItem $pattern -ErrorAction SilentlyContinue | Select-Object -First 1
        if ($found) {
            Write-Host "     💡 找到 ohpm: $($found.FullName)" -ForegroundColor Yellow
            Write-Host "     需要将此目录添加到 PATH: $($found.Directory.FullName)" -ForegroundColor Yellow
            break
        }
    }
}

# 检查 hvigorw
Write-Host ""
if (Test-Path ".\hvigorw.bat") {
    Write-Host "  ✅ hvigorw.bat 存在" -ForegroundColor Green
} else {
    Write-Host "  ❌ hvigorw.bat 不存在" -ForegroundColor Red
}

Write-Host ""
Write-Host "================================" -ForegroundColor Cyan
Write-Host "3. 建议操作" -ForegroundColor Yellow
Write-Host ""

if ($null -eq $hdcPath -or $null -eq $ohpmPath) {
    Write-Host "需要配置环境变量：" -ForegroundColor Yellow
    Write-Host "1. 打开'系统属性' -> '环境变量'" -ForegroundColor White
    Write-Host "2. 在'用户变量'中找到或创建 'Path' 变量" -ForegroundColor White
    Write-Host "3. 添加 DevEco Studio SDK 的 toolchains 目录，通常是：" -ForegroundColor White
    Write-Host "   C:\Users\你的用户名\Huawei\Sdk\openharmony\版本号\toolchains" -ForegroundColor Cyan
    Write-Host "4. 重启 PowerShell 或 命令提示符" -ForegroundColor White
    Write-Host ""
    Write-Host "或者在 DevEco Studio 中：" -ForegroundColor Yellow
    Write-Host "1. 打开 DevEco Studio" -ForegroundColor White
    Write-Host "2. File -> Settings -> Build, Execution, Deployment -> Build Tools" -ForegroundColor White
    Write-Host "3. 查看 SDK Location 和 Node.js Location" -ForegroundColor White
    Write-Host "4. 将这些路径的 toolchains 子目录添加到系统 PATH" -ForegroundColor White
} else {
    Write-Host "✅ 环境配置正常，可以运行构建脚本！" -ForegroundColor Green
}

Write-Host ""
Write-Host "4. 检查设备连接..." -ForegroundColor Yellow
if ($hdcPath) {
    Write-Host ""
    & hdc list targets
} else {
    Write-Host "  ⏭️  跳过（hdc 不可用）" -ForegroundColor DarkGray
}

Write-Host ""
Write-Host "================================" -ForegroundColor Cyan
Write-Host ""

